/**
 * 地图服务 
 */
export default {
	// 监听网络
	getSetting(){
		return new Promise((resolve, reject) => {
		    uni.getSetting({
		        success: (res) => {
		            if (res.authSetting['scope.userLocation'] === undefined) {
		                resolve(0);
		                return;
		            }
		            if (res.authSetting['scope.userLocation']) {
		                resolve(1);
		            } else {
		                resolve(2);
		            }
		        }
		    })
		})
	},
	async getLocation(cb){
		// #ifdef APP-PLUS
		let status = await this.checkPermission();
		if (status !== 1) {
		    return;
		}
		// #endif
		// #ifdef MP-WEIXIN || MP-TOUTIAO || MP-QQ
		let status = await this.getSetting();
		if (status === 2) {
		    this.showConfirm();
		    return;
		}
		// #endif
		
		this.doGetLocation(cb)
	},
	doGetLocation(cb=false){
		uni.getLocation({
			type: 'wgs84',
		    success: (res) => {
		        // this.hasLocation = true;
		        // this.location = formatLocation(res.longitude, res.latitude);
				cb&&cb(res)
		    },
		    fail: (err) => {
				console.log(err)
		        // #ifdef MP-BAIDU
		        if (err.errCode === 202 || err.errCode === 10003) { // 202模拟器 10003真机 user deny
		            this.showConfirm();
		        }
		        // #endif
		        // #ifndef MP-BAIDU
		        if (err.errMsg.indexOf("auth deny") >= 0) {
		            uni.showToast({
		                title: "访问位置被拒绝"
		            })
		        } else {
		            uni.showToast({
		                title: err.errMsg
		            })
		        }
		        // #endif
				cb(false)
		    }
		})
	}
}